//
//  UIScrollView+RefreshView.m
//  Liker
//
//  Created by iBenz on 15/6/15.
//  Copyright (c) 2015年 Aoe. All rights reserved.
//

#import "UIScrollView+RefreshView.h"
#import <objc/runtime.h>

@implementation UIScrollView (PullHandler)

- (void)setPullDownHandler:(PulldownHandler)handler {
    if (!handler) {
        self.mj_header = nil;
    }
    else {
        __weak UIScrollView *weak = self;
        self.mj_header = [RefreshHeader headerWithRefreshingBlock:^{
            if(handler){
                handler(weak);
            }
        }];
    }
    
    self.mj_header.automaticallyChangeAlpha = YES;
}

-(void)setPullUpHandler:(PullupHandler)handler {
    if (!handler) {
        self.mj_footer = nil;
    }
    else {
        __weak UIScrollView *weak = self;
        self.mj_footer = [RefreshFooter footerWithRefreshingBlock:^{
            if (handler) {
                handler(weak);
            }
        }];
//        self.mj_footer.automaticallyHidden = YES;
    }
}

- (void)endPulllAndMarkAsNomoreData:(BOOL)nomore {
    if (self.mj_header.isRefreshing) {
        [self.mj_header endRefreshing];
    }
    if (self.mj_footer) {
        if (nomore) {
            [self.mj_footer endRefreshingWithNoMoreData];
        }
        else {
            if (self.mj_footer.state==MJRefreshStateNoMoreData) {
                [self.mj_footer resetNoMoreData];
            }
            if (self.mj_footer.isRefreshing) {
                [self.mj_footer endRefreshing];
            }
        }
        self.mj_footer.hidden = nomore;
    }
    
    static const char kNomoreKey = '\0';
    UILabel *nomoreLabel = objc_getAssociatedObject(self, &kNomoreKey);
    if (!nomoreLabel) {
        nomoreLabel = [[UILabel alloc] init];
        objc_setAssociatedObject(self, &kNomoreKey, nomoreLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        nomoreLabel.font = T2(14);
        nomoreLabel.textColor = ({THEME; theme.c3;});
        nomoreLabel.textAlignment = NSTextAlignmentCenter;
        nomoreLabel.text = @"没有更多了";
        [self addSubview:nomoreLabel];
    }
    nomoreLabel.hidden = !nomore;
    if (nomore) {
        CGRect frame = nomoreLabel.bounds;
        frame.size.width = CGRectGetWidth(self.bounds);
        frame.size.height = 30;
        frame.origin.y = MAX(self.contentSize.height, CGRectGetHeight(self.bounds));
        nomoreLabel.frame = frame;
    }
    
}

- (void)autoPulldown {
    [self.mj_header beginRefreshing];
}

@end


#pragma mark - RefreshHeader

@interface RefreshHeader () {
}
@property (nonatomic, strong)UIImageView *animateView;
@property (nonatomic, strong)CABasicAnimation *animation;

@end
@implementation RefreshHeader

- (void)prepare{
    [super prepare];
    [self addSubview:self.animateView];
}

-(void)placeSubviews{
    [super placeSubviews];
    _animateView.center = (CGPoint){CGRectGetWidth(self.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0};
}

-(void)setState:(MJRefreshState)state{
    MJRefreshCheckState;
    switch (state) {
        case MJRefreshStateIdle:
        {
            [self endRotation];
        }
            break;
        case MJRefreshStatePulling :
        {
            [self beginRotating];
        }
            break;
        case MJRefreshStateRefreshing:
        {
            [self beginRotating];
        }
            break;
            
        default:
            break;
    }
}

- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
}

- (void)beginRotating{
    [self.animateView startAnimating];
}

- (void)endRotation{
    [_animateView stopAnimating];
}

- (UIImageView *)animateView {
    if (!_animateView) {
        _animateView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"refresh_view_1"]];
        [_animateView sizeToFit];
        NSMutableArray *aImgs = [NSMutableArray array];
        for (NSInteger i = 1; i < 7; i++) {
            [aImgs addObject:[UIImage imageNamed:[NSString stringWithFormat:@"refresh_view_%ld", (long)i]]];
        }
        _animateView.animationImages = aImgs;
        _animateView.animationDuration = 1.0;
    }
    return _animateView;
}

@end

#import "ECLoadingIndicator.h"
@interface RefreshFooter () {
}
@property (nonatomic, strong) ECLoadingIndicator *idv;
@end
@implementation RefreshFooter

- (void)prepare{
    [super prepare];
    [self addSubview:self.idv];
}

-(void)placeSubviews{
    [super placeSubviews];
    _idv.center = (CGPoint){CGRectGetWidth(self.bounds)/2.0, CGRectGetHeight(self.bounds)/2.0};
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
        [self endRotation];
    } else if (state == MJRefreshStateRefreshing) {
        [self beginRotating];
    }
}

- (void)beginRotating{
    [self.idv startAnimating];
}

- (void)endRotation{
    [_idv stopAnimating];
}

- (ECLoadingIndicator *)idv {
    if (!_idv) {
        _idv = [[ECLoadingIndicator alloc] initWithLogo:nil];
        _idv.hidesWhenStopped = YES;
    }
    return _idv;
}

@end
